﻿/*
 * Copyright (c) 2011 http://DesignBased.NET 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the 
 * Software  * without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using T163.Open.Api.Model;

//  http://stackoverflow.com/questions/3517172/can-i-use-all-c-4-0-features-in-a-project-that-targets-net-3-5

namespace T163.Open.Api.Sample
{
    public partial class Form1 : Form
    {
        private string consumerKey = "";  // add consumerKey
        private string consumerSecret = ""; // add consumerSecret

        private OAuthService oauthService;

        public Form1()
        {
            InitializeComponent();

            oauthService = new OAuthService(consumerKey, consumerSecret);

            oauthService.GetRequestToken();

            string authorizationUrl = oauthService.GetAuthorizationURL();

            webBrowser1.Navigate(authorizationUrl);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string verifier = txtPinCode.Text;

            if (string.IsNullOrEmpty(verifier))
            {
                txtPinCode.Focus();
                return;
            }

            oauthService.GetAccessToken(verifier);

            Users user = oauthService.VerifyCredentials();

            lblUser.Text = user.ScreenName;

            this.webBrowser1.Visible = false;
            this.richTextBox1.Visible = true;

            AutoTest();
        }

        private void DisplayText(params string[] text)
        {
            richTextBox1.SelectedText = string.Empty;
            richTextBox1.AppendText(string.Join("    ", text));
            richTextBox1.ScrollToCaret();
        }

        private void AutoTest()
        {
            //// 发布微博

            this.DisplayText("****************测试 Update********************\n");
            Statues status = oauthService.Update(new Statues { Text = "测试测试测试呵呵a" });
            this.DisplayText(status.User.ScreenName,
                DateTime.ParseExact(status.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), status.Text, "\n\n");

            this.DisplayText("****************测试 GetPublicTimeLine********************\n");
            List<Statues> ptllst = oauthService.GetPublicTimeLine(true);
            foreach (var s in ptllst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("****************测试 GetHomeTimeLine********************\n");
            List<Statues> htllst = oauthService.GetHomeTimeLine(5);
            foreach (var s in htllst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 GetRetweets********************\n");
            List<Statues> rtlst = oauthService.GetRetweets("-4832355922500582950", 5);
            foreach (var s in rtlst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }


            this.DisplayText("\n****************测试 GetMentions********************\n");
            List<Statues> mtlst = oauthService.GetMentions(2);
            foreach (var s in mtlst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 GetUserTimeLine********************\n");

#if !NET20
            List<Statues> utllst = oauthService.GetUserTimeLine(name: "Jeek", count: 5);
#else
            List<Statues> utllst = oauthService.GetUserTimeLine("Jeek", false, 5);
#endif
            foreach (var s in utllst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 GetRetweetsOfMe********************\n");
            List<Statues> romlst = oauthService.GetRetweetsOfMe(3);
            foreach (var s in romlst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 GetCommentByMe********************\n");
            List<Statues> cmtblst = oauthService.GetCommentsByMe(3);
            foreach (var s in cmtblst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 GetCommentToMe********************\n");
            List<Statues> cmttlst = oauthService.GetCommentsToMe(3);
            foreach (var s in cmttlst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n");
            }

            this.DisplayText("\n****************测试 Reply********************\n");
            Statues restatues = oauthService.Reply(status.ID, new Statues { Text = "由程序发的评论" }, isRetweet: "1");
            this.DisplayText(restatues.User.ScreenName,
                DateTime.ParseExact(restatues.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), restatues.Text, "\n\n");

            this.DisplayText("****************测试 Show********************\n");
            Statues sstatues = oauthService.Show(status.ID);
            this.DisplayText(sstatues.User.ScreenName,
                DateTime.ParseExact(sstatues.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), sstatues.Text, "\n\n");

            this.DisplayText("****************测试 Destroy********************\n");
            Statues dstatues = oauthService.Destroy(status.ID);
            this.DisplayText(dstatues.User.ScreenName,
                DateTime.ParseExact(dstatues.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), dstatues.Text, "\n\n");

            //// 发布图片微博
            richTextBox1.AppendText("****************测试发布图片微博********************");
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
#if !NET20
                Statues picSt = oauthService.Upload(openFileDialog1.FileName, "微博带图片");
#else
                Statues picSt = oauthService.Upload(openFileDialog1.FileName, null, "微博带图片");
#endif
                richTextBox1.AppendText(picSt.User.ScreenName + "  " + DateTime.ParseExact(picSt.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString() + "  " + picSt.Text);
            }


            this.DisplayText("\n\n**********************测试 userShow*******************\n");
            Users usersLst = oauthService.UserShow("2009188438");
            this.DisplayText(usersLst.Name, (usersLst.Gender == "0" ? "女" : "男"), usersLst.Description, "\n\n");

            this.DisplayText("**********************测试 Friends*******************\n");

#if !NET20
            List<Users> fdsLst = oauthService.Friends(screenName: "Jeek");
#else
            List<Users> fdsLst = oauthService.Friends("2009188438");
#endif
            foreach (var u in fdsLst)
            {
                this.DisplayText(u.Name, (u.Gender == "0" ? "女" : "男"), u.Description, "\n");
            }


            this.DisplayText("\n**********************测试 Followers*******************\n");
#if !NET20
            List<Users> flsLst = oauthService.Followers(screenName: "Jeek");
#else
            List<Users> flsLst = oauthService.Followers("2009188438");
#endif
            foreach (var u in flsLst)
            {
                this.DisplayText(u.Name, (u.Gender == "0" ? "女" : "男"), u.Description, "\n");
            }


            this.DisplayText("\n**********************测试 我喜欢*******************\n");
            List<Users> sggLst = oauthService.Suggestions(5);
            foreach (var u in sggLst)
            {
                this.DisplayText(u.Reason, u.Name, (u.Gender == "0" ? "女" : "男"), u.Description, "\n\n");
            }

            this.DisplayText("**********************测试 Friendship Create*******************\n");
#if !NET20
            Users fscLst = oauthService.FriendshipsCreate(screenName: "2009188438");
#else
            Users fscLst = oauthService.FriendshipsCreate("2009188438");
#endif
            this.DisplayText(usersLst.Name, (fscLst.Gender == "0" ? "女" : "男"), fscLst.Description, "\n\n");


            this.DisplayText("**********************测试 Friendship Show*******************\n");
#if !NET20
            Friendship fdship = oauthService.FriendshipsShow(targetScreenName: "2009188438");
#else
            Friendship fdship = oauthService.FriendshipsShow(null, "2009188438");
#endif
            this.DisplayText("我已关注", fdship.Target.ScreenName, "？", fdship.Target.FollowedBy ? "是" : "否", "\n");
            this.DisplayText(fdship.Target.ScreenName, "已关注我？", fdship.Target.Following ? "是" : "否", "\n\n");

            this.DisplayText("**********************测试 top retweets*******************\n");
#if !NET20
            List<TopRetweets> tpwlst = oauthService.TopRetweets(size: 3);
#else
            List<TopRetweets> tpwlst = oauthService.TopRetweets(5);
#endif
            foreach (var t in tpwlst)
            {
                this.DisplayText("新增转发", t.Count.ToString(), t.Status.Text, "\n");
            }

            this.DisplayText("\n**********************测试 trends*******************\n");
            List<Trend> tdlst = oauthService.TrendsRecommended();
            foreach (var t in tdlst)
            {
                this.DisplayText(t.Name, t.Query ?? t.Url, "\n");
            }

            this.DisplayText("\n**********************测试 Direct Message*******************\n");
            List<DirectMessage> dmlst = oauthService.DirectMessages();
            foreach (var t in dmlst)
            {
                this.DisplayText(t.Sender.ScreenName, t.Text, "\n");
            }

            this.DisplayText("\n**********************测试 Direct Message New*******************\n");
            DirectMessage dMsg = oauthService.DirectMessageNew("usingsystem", "这个是私信。");
            this.DisplayText("接收者：", dMsg.Recipient.Name, "发送者", dMsg.Sender.Name, "\n\n");


            this.DisplayText("****************测试 收藏********************\n");
#if !NET20
            List<Statues> fvlst = oauthService.Favorites(id: "Jeek", count: 2);
#else
            List<Statues> fvlst = oauthService.Favorites("Jeek");
#endif
            foreach (var s in fvlst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n\n");
            }


            this.DisplayText("****************测试 搜索微博********************\n");
#if !NET20
            List<Statues> sslst = oauthService.SearchStatus(q:"屁");
#else
            List<Statues> sslst = oauthService.SearchStatus("屁");
#endif
            foreach (var s in sslst)
            {
                this.DisplayText(s.User.ScreenName,
                    DateTime.ParseExact(s.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), s.Text, "\n\n");
            }


            this.DisplayText("**********************测试 搜索用户*******************\n");
#if !NET20
            List<Users> suLst = oauthService.SearchUser(q: "Jeek");
#else
            List<Users> suLst = oauthService.SearchUser("Jeek");
#endif
            foreach (var u in suLst)
            {
                this.DisplayText(u.Name, (u.Gender == "0" ? "女" : "男"), u.Description, "\n");
            }
        }

        private void BtnPublice_Click(object sender, EventArgs e)
        {
            if (chkPic.Checked == false)
            {
                this.DisplayText("****************发布微博********************\n");
                Statues status = oauthService.Update(new Statues { Text = txtText.Text });
                this.DisplayText(status.User.ScreenName,
                    DateTime.ParseExact(status.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString(), status.Text, "\n\n");
            }
            else
            {
                richTextBox1.AppendText("****************发布图片微博********************");
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
#if !NET20
                    Statues picSt = oauthService.Upload(openFileDialog1.FileName, txtText.Text);
#else
                    Statues picSt = oauthService.Upload(openFileDialog1.FileName, null, txtText.Text);
#endif
                    richTextBox1.AppendText(picSt.User.ScreenName + "  " + DateTime.ParseExact(picSt.CreatedAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture).ToString() + "  " + picSt.Text);
                }
            }

        }
    }
}
